package com.yuyou.fn.educational.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.IAttendanceRecordService;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.educational.vo.StudentRefundQueryVo;
import com.yuyou.fn.educational.vo.StudentRefundSaveOrUpdateVo;
import com.yuyou.fn.settlement.service.ISettlementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.StudentRefundMapper;
import com.yuyou.fn.educational.entity.StudentRefund;
import com.yuyou.fn.educational.service.IStudentRefundService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * StudentRefund 表数据服务层接口实现类
 *
 */
@Service
public class StudentRefundServiceImpl extends SuperServiceImpl<StudentRefundMapper, StudentRefund,Long> implements IStudentRefundService {

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private ISettlementService settlementService;

    @Resource
    private IAttendanceRecordService attendanceRecordService;

    @Override
    public void deleteStudentRefund(Long studentRefundId) {deleteById(studentRefundId);}

    @Transactional
    @Override
    public void saveOrUpdateStudentRefund(StudentRefundSaveOrUpdateVo studentRefundSaveOrUpdateVo) {
        studentRegService.changeRefundStatus(studentRefundSaveOrUpdateVo.getRegId() , 1 );
        StudentRefund studentRefund = new StudentRefund();

        StudentReg studentReg = studentRegService.findById(studentRefundSaveOrUpdateVo.getRegId());

        BeanUtils.copyProperties(studentRefund,studentRefundSaveOrUpdateVo);


        studentRefund.setStudentRegId(studentReg.getRegId());
        studentRefund.setAreaTeamId(studentReg.getAreaTeamId());
        studentRefund.setBusTeamId(studentReg.getBusTeamId());
        studentRefund.setStudentId(studentReg.getStudentId());
        studentRefund.setStudentName(studentReg.getStudentName());
        studentRefund.setClassName(studentReg.getClassName());
        studentRefund.setStatus(0);
        List<Integer> lectureNos = new ArrayList<>(20);
        for(int i=studentRefundSaveOrUpdateVo.getStartAmount();i<=studentRefundSaveOrUpdateVo.getEndAmount();i++){
            lectureNos.add(i );
        }
        studentRefund.setRefundLecture(StringUtils.join(lectureNos,","));
        int refundLectureAmount = studentRefundSaveOrUpdateVo.getStartAmount() - studentRefundSaveOrUpdateVo.getEndAmount() + 1 ;
        int regLectureAmount = studentReg.getStartAmount() - studentReg.getEndAmount() + 1 ;
        studentRefund.setAmount(Utils.calProportion(studentReg.getTotalAmount(),new BigDecimal(refundLectureAmount),new BigDecimal(regLectureAmount)));

        if(studentRefund.getStudentRefundId() == null ){
            studentRefund.setCreateTime(TimeUtils.now());
            studentRefund.setUpdateTime(TimeUtils.now());
            insertSelective(studentRefund);

        }else{
            studentRefund.setUpdateTime(TimeUtils.now());
            updateSelectiveById(studentRefund);

        }
        
    }

    @Override
    public Page<StudentRefund> findStudentRefundList(StudentRefundQueryVo studentRefundQueryVo,int pageNo,int pageSize) {

        Example<StudentRefund> studentRefundExample = StudentRefund.createExample();
        studentRefundExample.orderBy("createTime").desc();

        Example.Criteria criteria = studentRefundExample.createCriteria();

        CriteriaUtils.fillCondition(criteria,studentRefundQueryVo);

        return findPageByExample(studentRefundExample,pageNo, pageSize );
    }

    @Transactional
    @Override
    public void changeStatus(Long studentRefundId, Integer status, String returnResult) {
        StudentRefund update = new StudentRefund();
        update.setStudentRefundId(studentRefundId);
        update.setStatus(status);
        update.setReturnResult(returnResult);
        boolean b = updateSelectiveById(update);
        if(b){
            StudentRefund studentRefund = findById(studentRefundId);
            studentRegService.changeRefundStatus(studentRefund.getStudentRegId(), 2);
            studentRegService.changeRegStatus(studentRefund.getStudentRegId(), 3);
            if(status == 1 ){
                CourseClass courseClass = courseClassService.findById(studentRefund.getClassId());
                courseClassService.decreaseRegAmount(studentRefund.getClassId());
                settlementService.bookRefundForCourse( courseClass.getBusTeamId(), studentRefund.getStudentId() , studentRefund.getAmount() , courseClass.getClassName()  ,courseClass.getClassId() , studentRefund.getStudentRegId() );
                String[] lectureNos = studentRefund.getRefundLecture().split(",");
                attendanceRecordService.deleteAttendanceRecordForStudent(studentRefund.getClassId(),studentRefund.getStudentId(),Integer.parseInt(lectureNos[0]),Integer.parseInt(lectureNos[lectureNos.length -1 ]));
            }
        }


    }


}